import MyCLibrary.*
import dream_lab.io.UvLoop
import dream_lab.io.runInterval
import kotlinx.cinterop.*

fun main() {
    val message = "Message from C++"
    MyCLibrary.runSelf {
        println("$message is: $it")
    }
    println("Hello, Kotlin/Native!")
    val loop = UvLoop.default
    var counter = 0
    loop.runInterval(1000) {
        println("Counter: ${counter++}")
        if (counter > 10) it.stop()
    }
    println("Timer is running...")
    loop.run()
    println("Over.")
}

class MyCLibrary {
    companion object {
        fun runSelf(action: (message: String) -> Unit) {
            val actionRef = StableRef.create(action)
            runSelf(actionRef.asCPointer(), staticCFunction { message, callback ->
                val _actionRef = callback!!.asStableRef<(message: String) -> Unit>()
                val _action = _actionRef.get()
                _action(message!!.toKString())
            })
            actionRef.dispose()
        }
    }
}


